package com.amumu.netctoss.interceptor;

import java.util.Set;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

@SuppressWarnings("serial")
public class LoginInterceptor implements Interceptor{
	
	private String key;
	private String errorResult;
	
	public void setKey(String key) {
		this.key = key;
	}

	public void setErrorResult(String errorResult) {
		this.errorResult = errorResult;
	}

	public void destroy() {
		
	}

	public void init() {
		
	}

	public String intercept(ActionInvocation ai) throws Exception {
		ActionContext ctx = ai.getInvocationContext();
		if(key!=null&&ctx.getSession().get(key)==null){
			return errorResult;
		}
		String uri = ServletActionContext.getRequest().getServletPath();
		//System.out.println("uri:"+uri);
		if(uri.startsWith("/index")||uri.startsWith("/user")){
			return ai.invoke();
		}
		Set<String> urls = (Set<String>) ctx.getSession().get("user.urls");
		if(!urls.contains(uri)){
			return "nopower";
		}
		return ai.invoke();
	}

}
